﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using G.Navigate;
using TianYa.API.Response;
using TianYa.API.Request;

namespace TianYa.View
{
    public partial class PlatePostListPage : GBasePage
    {
        public PlatePostListPage()
        {
            InitializeComponent();
        }
        string plateId;
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (listbox.ItemsSource == null)
            {

                plateId = this.NavigationContext.QueryString["plateid"];
                PageTitle.Text = this.NavigationContext.QueryString["plateName"];
                LoadData(null);
            }
        }

        private void LoadData(string nextTime)
        {
            this.RegisterLoading("正在加载帖子列表...");
            GloabalData.Instance.APIClient.Execute<GetPlatePostListResponse>(new GetPlatePostListRequest(plateId, nextTime, 100), (c) =>
            {
                this.Dispatcher.BeginInvoke(() =>
                {
                    this.listbox.ItemsSource = c.PostInfos;
                    this.UnregisterLoading();
                });
            });
        }

        private void listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listbox.SelectedItem != null)
            {
                API.Domain.PostInfo info = listbox.SelectedItem as API.Domain.PostInfo;
                this.NavigationService.Navigate(string.Format("/View/PostContenPage.xaml?plateid={0}&postid={1}&name={2}", plateId, info.Id, HttpUtility.UrlEncode(info.Title)));
                listbox.SelectedItem = null;
            }
        }
        int currentPage = 1;
        private void btnProview_Click(object sender, EventArgs e)
        {
            if (this.listbox.ItemsSource != null)
            {
                var post = (this.listbox.ItemsSource as TianYa.API.Domain.PostInfo[]).LastOrDefault();
                if (post != null)
                {
                    LoadData(post.ReplyTime);
                }
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (this.listbox.ItemsSource != null)
            {
                var post = (this.listbox.ItemsSource as TianYa.API.Domain.PostInfo[]).FirstOrDefault();
                if (post != null)
                {
                    LoadData(post.ReplyTime);
                }
            }
          
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            LoadData(null);
        }

        private void btnFavirate_Click(object sender, EventArgs e)
        {
            var d = GloabalData.Instance.TianYaDataBase.FavoritPlateTable.FirstOrDefault(c => c.PlateId == plateId);
            if (d == null)
            {
                GloabalData.Instance.TianYaDataBase.FavoritPlateTable.InsertOnSubmit(new Data.FavoritPlate() { PlateId = plateId, Name = this.NavigationContext.QueryString["plateName"] });
                GloabalData.Instance.TianYaDataBase.SubmitChanges();
            }
            this.ShowState("收藏成功");
        }
    }
}